package com.geostar.constructionland.analyse.service.handler.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.geostar.constructionland.analyse.entity.HttpResult;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResult;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResultDqhp;
import com.geostar.constructionland.analyse.entity.model.AnalyseModel;
import com.geostar.constructionland.analyse.entity.resulthandler.HandlerResult;
import com.geostar.constructionland.analyse.mapper.sptalyresult.AlyResultXzdwMapper;
import com.geostar.constructionland.analyse.service.analyseresult.AlyResultDqhpService;
import com.geostar.constructionland.analyse.service.handler.IResultHandler;
import com.geostar.constructionland.analyse.service.handler.impl.util.XmlUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author yelll
 * @since 2020-6-18 20:16
 **/
@Component
@Slf4j
public class DqhpHandler implements IResultHandler {
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    AlyResultDqhpService dqhpService;
    @Autowired
    AlyResultXzdwMapper alyResultXzdwMapper;
    @Override
    public HandlerResult handler(AnalyseModel analyseModel, HttpResult httpResult) throws Exception {
        HandlerResult result = HandlerResult.createBySuccess();
        log.info("处理行政区占压返回数据,状态码:{}",httpResult.getCode());
        if(HttpStatus.SC_OK==httpResult.getCode()){
            String newBody = XmlUtil.parseXml(httpResult.getBody());
            httpResult.setBody(newBody);
            /**保存占压信息**/
            AlyResult alyResult = objectMapper.readValue(httpResult.getBody(),AlyResult.class);
            log.info("body:"+httpResult.getBody());
            if(HttpStatus.SC_OK == alyResult.getStatus()){
                /**如果成功,构建主体解析**/
                List<AlyResultDqhp> alyResultDqhps = objectMapper.readValue(alyResult.getAnalysisResult().getResultRecords(),
                        new TypeReference<List<AlyResultDqhp>>(){});
                if(alyResultDqhps.size()>0){
                    alyResultDqhps.forEach(dqhp -> {
                        String dqhpId = UUID.randomUUID().toString().replace("-","");
                        dqhp.setFxid(dqhpId);
                        dqhp.setFxbs(analyseModel.getAlyId());
                        if(dqhp.getXzdwInfos().size()>0){
                            dqhp.getXzdwInfos().forEach(xzdw -> {
                                xzdw.setFxbs(analyseModel.getAlyId());
                                xzdw.setDqhpid(dqhpId);
                                xzdw.setXzdwid(UUID.randomUUID().toString().replace("-",""));
                                alyResultXzdwMapper.insert(xzdw);
                            });
                        }
                    });
                    dqhpService.saveBatch(alyResultDqhps);
                }
            }else{
                throw new Exception(alyResult.getMessage());
            }
        }
        return result;
    }
}
